package com.happy_lab_management.demo.admin.labBorrowApplication.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.happy_lab_management.context.BaseContext;
import com.happy_lab_management.demo.admin.labBorrowApplication.service.LabBorrowApplicationService;
import com.happy_lab_management.demo.admin.semester.service.SemesterService;
import com.happy_lab_management.demo.admin.user.service.StudentService;
import com.happy_lab_management.demo.common.section.service.SectionService;
import com.happy_lab_management.demo.common.weekday.service.WeekdayService;
import com.happy_lab_management.demo.technician.lab.service.LabService;
import com.happy_lab_management.entity.DTO.LabBorrowApplicationDTO;
import com.happy_lab_management.entity.*;
import com.happy_lab_management.entity.VO.EquipmentRepairVO;
import com.happy_lab_management.entity.VO.LabBorrowApplicationVO;
import com.happy_lab_management.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/admin/labBorrow")
@RestController("adminLabBorrowController")
public class LabBorrowApplicationController {
    @Autowired
    private LabBorrowApplicationService labBorrowApplicationService;
    @Autowired
    private LabService labService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private SemesterService semesterService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private WeekdayService weekdayService;


    @RequestMapping("/list")
    public Result<IPage<LabBorrowApplicationVO>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "50") Integer pageSize
    ) {
        QueryWrapper<LabBorrowApplication> queryWrapper = new QueryWrapper<>();
        //实验室编号

        Page<LabBorrowApplication> page = new Page<>(pageNo, pageSize);
        IPage<LabBorrowApplication> pageList = labBorrowApplicationService.page(page, queryWrapper);

        List<LabBorrowApplicationVO> vos = pageList.getRecords().stream().map(er -> {
            LabBorrowApplicationVO vo = new LabBorrowApplicationVO();
            vo.setId(er.getId());

            //学期
            vo.setSemesterName(semesterService.getById(er.getSemesterId()).getSemesterName());
            //学生
            vo.setStudentName(studentService.getById(er.getStudentId()).getName());
            //实验室编号
            vo.setLabNumber(labService.getById(er.getLabId()).getLabNumber());
            //周次
            vo.setWeek(er.getWeek());
            //weekday
            vo.setWeekdayName(weekdayService.getById( er.getWeekday()).getWeekdayName());
            //节次
            vo.setSectionName(sectionService.getById(er.getSectionId()).getSectionName());
            //申请日期
            vo.setApplicationDate(er.getApplicationDate());
            //申请原因
            vo.setReason(er.getReason());
            //申请状态
            vo.setStatus(er.getStatus());

            return vo;
        }).collect(Collectors.toList());
        Page<LabBorrowApplicationVO> voPage = new Page<>();
        voPage.setRecords(vos);
        voPage.setTotal(pageList.getTotal());
        voPage.setSize(pageList.getSize());
        voPage.setCurrent(pageList.getCurrent());
        return Result.success(voPage);


    }
    /**
     * 设置维修状态
     * @param id 报修单ID
     * @param status 维修状态（可选参数）
     * @return 返回操作结果
     */
    @PostMapping("/updateStatus")
    public Result<String> updateRepairStatus(
            @RequestParam Integer id,
            @RequestParam(required = false) String status
    ) {
        LabBorrowApplication labBorrowApplication = labBorrowApplicationService.getById(id);

        if (status != null) {
            labBorrowApplication.setStatus(status);
        }

        labBorrowApplicationService.updateById(labBorrowApplication);

        return Result.success("设置报修状态成功");
    }


}
